using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Hook : MonoBehaviour
{
    public float rotationSpeed=100; // 钩子旋转速度
    public int rotateDir; // 是否顺时针旋转
    public bool isMoving; // 是否正在移动
    public float moveSpeed = 5; // 钩子移动速度
    private float moveTimer; // 移动计时器
    private bool returnToInitPos;//钩子是否在返回的状态
    private Vector3 originalPosition; // 钩子的初始位置
    /*void Start()
    {
 // 记录钩子的初始位置
        originalPosition = transform.position;
    }
    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMoving = true;
        }
        if (isMoving)
        {
// 钩子向下移动
            if (!returnToInitPos)
           {
                HookMove();
            }
//钩子返回
            else
            {
                HookReturn();
            }
        }
        else
        {
           HookRotate();
        }
    }*/
    private void HookRotate()
    {
        float angle = (transform.eulerAngles.z + 180) % 360 - 180;
        if (angle >= 60)
        {
            rotateDir = -1;
        }
        else if (angle <= -60)
        {
            rotateDir = 1;
        }
        transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime * rotateDir);
    }
    //绳索制作，绳索的精灵渲染器
    public SpriteRenderer sr;
    private void HookMove()
    {
        transform.Translate(Vector3.down * moveSpeed * Time.deltaTime);
        moveTimer += Time.deltaTime;
        //做绳子
        sr.size = new Vector2(sr.size.x, sr.size.y - moveSpeed * Time.deltaTime);

        if (moveTimer >= 2)
        {
            returnToInitPos = true;
        }
    }
    private void HookReturn()
    {
        transform.position = Vector3.MoveTowards(transform.position, originalPosition, moveSpeed * Time.deltaTime);
        //做绳子
        sr.size = new Vector2(sr.size.x, sr.size.y + moveSpeed * Time.deltaTime);

        if (transform.position.y >= originalPosition.y)
        {
            isMoving = false;
            returnToInitPos = false;
            moveTimer = 0;
            //做绳子
            sr.size = new Vector2(sr.size.x, 0);

            moveSpeed = 5;
            if (catchGold)
            {
                DestoryGold();
            }
        }
    }
    //采金矿
    private Gold gold;
    public Transform hookHeadTrans;
    private bool catchGold; //抓到金子，拉上来之后要销毁
    //关于金子轴心点调整的问题,不能移动
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Gold"))
        {
            gold = collision.GetComponent<Gold>();
            gold.transform.SetParent(hookHeadTrans);
            gold.transform.localPosition = Vector3.zero;
            returnToInitPos = true;
            moveSpeed = gold.speed;
            catchGold = true;
            //10.
            money += gold.goldNum;
            //12.
            moneyText.text = money.ToString();
            //16.
            if (gold.goldNum>=500)
            {
                audioSource.PlayOneShot(bPriceClip);
            }
            else if (gold.goldNum<500&& gold.goldNum>=100)
            {
                audioSource.PlayOneShot(mPriceClip);
            }
            else
            {
                audioSource.PlayOneShot(sPriceClip);
            }
        }
    }
    /// 销毁金子
    private void DestoryGold()
    {
        catchGold = false;
        Destroy(gold.gameObject);
    }
    //10.抓到不同的物体增加不同金錢
    public int money;
    //调整钻石轴心点位置，介绍图片改轴心点的方式，说昨天做钩子的时候提过

    //11.显示分数到UI上 制作游戏面板UI 讲解UI跟普通游戏物体处于两个世界，但其实是100倍，然后因为我们是截图，所以我们要对上，正常的话不会这样

    //12.显示分数到UI上
    public Text moneyText;
    void Start()
    {
        // 记录钩子的初始位置
        originalPosition = transform.position;
        //12.
        moneyText.text = money.ToString();
    }
    //13.游戏倒计时与时间显示
    public Text timeText;
    public float gameTimer;
    private void Update()
    {
        //14.
        if (gameOver)
        {
            return;
        }
        if (gameTimer<=0)
        {
            JudgeGameResult();
        }
        //13.
        gameTimer -= Time.deltaTime;
        timeText.text = gameTimer.ToString("0");

        if (Input.GetMouseButtonDown(0)&&!isMoving)
        {
            isMoving = true;
            //16
            audioSource.PlayOneShot(startMoveClip);
        }
        if (isMoving)
        {
            // 钩子向下移动
            if (!returnToInitPos)
            {
                HookMove();
            }
            //钩子返回
            else
            {
                HookReturn();
            }
        }
        else
        {
            HookRotate();
        }
    }
    //14.游戏胜利与失败判定
    private  bool gameOver;
    public GameObject winPanelGo;
    public GameObject losePanelGo;
    public int goalMoney;
    public void JudgeGameResult()
    {
        gameOver = true;
        if (money>= goalMoney)
        {
            winPanelGo.SetActive(true);
        }
        else
        {
            losePanelGo.SetActive(true);
        }
    }
    //15.添加胜利音乐
    //16.添加其他音效
    public AudioSource audioSource;
    public AudioClip startMoveClip;
    public AudioClip bPriceClip;
    public AudioClip mPriceClip;
    public AudioClip sPriceClip;
}
